// pages/zpxx_fb/zpxx_fb.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*图片上传*/
    imgs: [],
    placeholder: '请选择',
    /*end*/
    
    //获取当前位置
    chooseAddress: '请点击获取当前位置',

    //招聘职位
    array: [
      '业务员/销售', 
      '服务员', 
      '司机', 
      '客服',
      '前台',
      '文员',
      '店长',
      '收银员',
      '导购',
      '店员/营业员',
      '迎宾',
      '经理/主管',
      ],
    index: 0,//默认显示位置
    
    //薪资
    array2: [
      '面议',
      '1000元以下',
      '1000-2000',
      '2000-3000',
      '3000-5000',
      '5000-8000',
      '8000-10000',
      '10000-15000',
      '15000元以上',
    ],
    index2: 0,//默认显示位置
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var user = wx.getStorageSync("userInfo")
    if (user) {
      var city = wx.getStorageSync("city")
      that.setData({
        user,
        city
      })
      app.wxRequest('get','/Samecity/Recruit/Position_salary', {}, (res) => {
        if (res.data.code == 1) {
          that.setData({
            positionData: res.data.data1,
            salaryData: res.data.data2,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }, (err) => {
        console.log(err)
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'none',
        duration: 2000
      })
      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/user/user'
        })
      }, 2000)
    }
    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //获取企业地图信息
  onChangeAddress: function () {
    var _page = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        _page.setData({
          chooseAddress: res.name,
          latitude: res.latitude,
          longitude: res.longitude,
          company_address:res.address
        });

      },
      fail: function (err) {
        console.log(err)
      }
    });
  },


  //招聘职位普通选择器：
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  //薪资选择器：
  bindPickerChange2: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value
    })
  },


  //科目选择器：
  bindPickerChange6: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index6: e.detail.value
    })
  },

  //图片上传
  chooseImg: function (e) {
    let that = this;
    var imgs = that.data.imgs;
    var imgsLength = imgs.length
    wx.chooseImage({
      count: 9 - imgsLength,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var filePaths = res.tempFilePaths
        var filePathsLength = filePaths.length;
        var failNum = 0;
        for (var i = 0; i < filePathsLength; i++) {
          wx.uploadFile({
            url: app.globalData.theUrl1 + '/Samecity/Upload/uploadFile',
            filePath: filePaths[i],
            name: 'fileData',
            formData: {},
            success: (successBack) => {
              var imgs = that.data.imgs;
              var data = JSON.parse(successBack.data)
              if (data.code == 0) {
                imgs.push(data.url);
                that.setData({
                  imgs: imgs
                })
              } else {
                failNum++;
              }
            },
            fail: (failBack) => {
              failNum++;
            },
          });
        }
        wx.showToast({
          title: '总共' + filePathsLength + '张上传成功,' + failNum + '张上传失败！',
          icon: 'none'
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var imgs = this.data.imgs;
    wx.previewImage({
      current: imgs[index],
      urls: imgs
    })
  },
  //立即发布
  fromSub(e){
    console.log(e.detail.value)
    var that = this
    var fromData = e.detail.value
    if(fromData.corporate_name == ''){
      wx.showToast({
        title: '请填写公司名称',
        icon:'none'
      })
    }else if(fromData.company_address == ''){
      wx.showToast({
        title: '请选择公司地址',
        icon:'none'
      })
    }else if(fromData.position == ''){
      wx.showToast({
        title: '请选择招聘职位',
        icon:'none'
      })
    }else if(fromData.salary == ''){
      wx.showToast({
        title: '请选择薪资',
        icon:'none'
      })
    }else if(fromData.sex == ''){
      wx.showToast({
        title: '请选择性别',
        icon:'none'
      })
    }else if(fromData.explain == ''){
      wx.showToast({
        title: '请输入说明',
        icon:'none'
      })
    }else if(that.data.imgs.length == 0){
      wx.showToast({
        title: '请上传图片',
        icon:'none'
      })
    }else if(fromData.name == ''){
      wx.showToast({
        title: '请输入联系人姓名',
        icon:'none'
      })
    }else if(fromData.tel == ''){
      wx.showToast({
        title: '请输入联系人电话',
        icon:'none'
      })
    }else{
      fromData.latitude = that.data.latitude
      fromData.longitude = that.data.longitude
      fromData.images = that.data.imgs.join(',')
      fromData.uid = that.data.user.id 
      fromData.city = that.data.city
      fromData.type = '1' 
      app.wxRequest('post','/Samecity/Recruit/add_recruit',fromData,(res)=>{
        if (res.data.code == 1) {
          wx.showToast({
            title: "发布成功",
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },(err)=>{
        console.log(err)
      })
    }
  },

})